// index.js
const app = getApp();
Page({
  data: {
    SwiperList:[],
    NoticeList:[],
    MiliTraining:[],
    displayMiliTraining:[],
    displayNoticeList:[]
},
 /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
   this.getSwiperList()
   this.getNotice()
   this.getMiliTraining()
  },
 openWebView() {
  wx.navigateTo({
    url: '/pages/webview/webview?url= https://www.gfbzb.gov.cn/' 
  });
 },
 openGUFEWebView() {
  wx.navigateTo({
    url: '/pages/webview/webview?url= https://rwb.gufe.edu.cn/' 
  });
 },
 goHome() {
  this.setData({
    showWebView: false
  });
  wx.switchTab({
    url: '/pages/index/index' // 替换为首页路径
  });
},
navigateTo(e) {
  const url = e.currentTarget.dataset.url;
  if (url) {
    wx.navigateTo({
      url: url,
      fail: (err) => {
        console.error('导航失败：', err);
        wx.showToast({
          title: '页面跳转失败',
          icon: 'none'
        });
      }
    });
  }
},

getSwiperList(){
    wx.request({
  url: `${app.globalData.baseUrl}/api/index/swiper`,
  method:'GET',
  success:(res)=>{
if(res.statusCode===200){
     this.setData({
       SwiperList : res.data.data
     });
   }else{
     console.error('请求失败，状态码:', res.statusCode);
   }
},
fail:(err) =>{
       console.log('请求失败:', err);
}
})
},
getNotice() {
  wx.request({
    url: `${app.globalData.baseUrl}/api/index/Notice`,
    method: 'GET',
    success: (res) => {
      if (res.statusCode=== 200) {
        this.setData({
          NoticeList: res.data.data,
          displayNoticeList:res.data.data.slice(0,3)
        });
      } else {
        wx.showToast({ title: '加载失败', icon: 'error' });
      }
    },
  });
},
getMiliTraining() {
  wx.request({
    url: `${app.globalData.baseUrl}/index/militraining`,
    method: 'GET',
    success: (res) => {
      if (res.statusCode === 200) {
        this.setData({
        MiliTraining:res.data.data,
        displayMiliTraining:res.data.data.slice(0,4)
        });
      } else {
        wx.showToast({ title: '加载失败', icon: 'error' });
      }
    },
    fail: (err) => {
      console.error('请求失败:', err);
      wx.showToast({ title: '网络错误', icon: 'error' });
      }
  });
},
handleSwiperClick(e) {
  const { id } = e.currentTarget.dataset;
  wx.navigateTo({
    url: `/pages/index/swiper-detail/swiper-detail?id=${id}`
  });
},
viewNotice(e) {
  const { id } = e.currentTarget.dataset;
  wx.navigateTo({
    url: `/pages/index/more-notice/notice-detail/notice-detail?id=${id}`
  });
},
viewMiliTraining(e) {
  const { id } = e.currentTarget.dataset;
  wx.navigateTo({
    url: `/pages/index/more-mili/MiliTraining-detail/MiliTraining-detail?id=${id}`
  });
},
goToMoremili(e) {
  const { id } = e.currentTarget.dataset;
  wx.navigateTo({
    url: '/pages/index/more-mili/more-mili?id=${id}'
  });
},
goToMorenotice(e) {
  const { id } = e.currentTarget.dataset;
  wx.navigateTo({
    url: '/pages/index/more-notice/more-notice?id=${id}'
  });
},
// 点击卡片播放视频
playVideo: function(e) {
  const url = e.currentTarget.dataset.url;
  wx.navigateTo({
    url: `/pages/index/video-player/video-player?url=${url}`
  });
}
})

